package com.ruoyi.luban.service.impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.luban.domain.*;
import com.ruoyi.luban.domain.vo.LoginUserAndTokenVo;
import com.ruoyi.luban.domain.vo.WxLoginUserVo;
import com.ruoyi.luban.mapper.PFileInfoMapper;
import com.ruoyi.luban.mapper.PSchoolMapper;
import com.ruoyi.luban.mapper.PWxLoginUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.luban.mapper.PUserAddressMapper;
import com.ruoyi.luban.service.IPUserAddressService;

/**
 * 微信用户地址Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-28
 */
@Service
public class PUserAddressServiceImpl implements IPUserAddressService 
{
    @Autowired
    private PUserAddressMapper pUserAddressMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PWxLoginUserMapper wxLoginUserMapper;
    @Autowired
    private PSchoolMapper schoolMapper;
    @Autowired
    private PFileInfoMapper fileInfoMapper;
    /**
     * 查询微信用户地址
     * 
     * @param id 微信用户地址主键
     * @return 微信用户地址
     */
    @Override
    public PUserAddress selectPUserAddressById(Long id)
    {
        return pUserAddressMapper.selectPUserAddressById(id);
    }

    /**
     * 查询微信用户地址列表
     * 
     * @param pUserAddress 微信用户地址
     * @return 微信用户地址
     */
    @Override
    public List<PUserAddress> selectPUserAddressList(PUserAddress pUserAddress)
    {
        return pUserAddressMapper.selectPUserAddressList(pUserAddress);
    }

    /**
     * 新增微信用户地址
     * 
     * @param pUserAddress 微信用户地址
     * @return 结果
     */
    @Override
    public int insertPUserAddress(PUserAddress pUserAddress)
    {
        pUserAddress.setCreateTime(DateUtils.getNowDate());
        return pUserAddressMapper.insertPUserAddress(pUserAddress);
    }

    /**
     * 修改微信用户地址
     * 
     * @param pUserAddress 微信用户地址
     * @return 结果
     */
    @Override
    public int updatePUserAddress(PUserAddress pUserAddress)
    {
        pUserAddress.setUpdateTime(DateUtils.getNowDate());
        return pUserAddressMapper.updatePUserAddress(pUserAddress);
    }

    /**
     * 批量删除微信用户地址
     * 
     * @param ids 需要删除的微信用户地址主键
     * @return 结果
     */
    @Override
    public int deletePUserAddressByIds(Long[] ids)
    {
        return pUserAddressMapper.deletePUserAddressByIds(ids);
    }

    /**
     * 删除微信用户地址信息
     * 
     * @param id 微信用户地址主键
     * @return 结果
     */
    @Override
    public int deletePUserAddressById(Long id)
    {
        return pUserAddressMapper.deletePUserAddressById(id);
    }

    /**
     * 新增微信用户地址
     *
     * @param pUserAddress 微信用户地址
     * @return 结果
     */
    @Override
    public LoginUserAndTokenVo addUserAddress(String token,LoginUserAndTokenVo loginInfo, PUserAddress pUserAddress)
    {
        if(pUserAddress.getId() != null){
            //更新
            pUserAddressMapper.updatePUserAddress(pUserAddress);
            return loginInfo;
        }
        pUserAddress.setCreateTime(DateUtils.getNowDate());
        //获取学校信息
        PSchool pSchool = schoolMapper.selectPSchoolById(pUserAddress.getSchoolId());
        if(pSchool == null){
            return null;
        }
        PWxLoginUser pWxLoginUser = wxLoginUserMapper.selectPWxLoginUserByOpenId(loginInfo.getMaOpenId());
        if(!pWxLoginUser.getStatus().equals("2")) {
            pWxLoginUser.setStatus("2");
            pWxLoginUser.setContactId(pSchool.getId());
            pWxLoginUser.setUpdateTime(new Date());
//            if("微信用户".equals(pWxLoginUser.getNickName())) {
                pWxLoginUser.setNickName(pSchool.getSchoolName());
                PFileInfo fileInfo = new PFileInfo();
                fileInfo.setContactId(pSchool.getId());
                List<PFileInfo> pFileInfos = fileInfoMapper.selectPFileInfoList(fileInfo);
                if (!pFileInfos.isEmpty()) {
                    pWxLoginUser.setLoginUrl(pFileInfos.get(0).getFileUrl());
                }
//            }
            wxLoginUserMapper.updatePWxLoginUser(pWxLoginUser);
        }
        loginInfo.setSchoolId(pSchool.getId());
        loginInfo.setSchoolName(pSchool.getSchoolName());
        loginInfo.setToBind("0");//不用绑定了
        String phone = pUserAddress.getPhone();
        WxLoginUserVo loginUser = loginInfo.getLoginUser();
        loginUser.setNickName(pWxLoginUser.getNickName());
        loginUser.setLoginUrl(pWxLoginUser.getLoginUrl());
        BeanUtils.copyProperties(pSchool,pUserAddress);
        pUserAddress.setOpenId(loginInfo.getMaOpenId());
        pUserAddress.setSchoolName(pSchool.getSchoolName());
        pUserAddress.setPhone(phone);
//        redisTemplate.opsForValue().set(token, JSON.toJSONString(loginInfo), 1, TimeUnit.DAYS);
        pUserAddressMapper.insertPUserAddress(pUserAddress);
        loginInfo.setSubscribeStatus(pWxLoginUser.getSubscribeStatus());
        return loginInfo;
    }
}
